<?php
namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use app\common\lib\Show;
use app\admin\validate\SuperUser as SuperUserValidate;
use think\exception\ValidateException;
use think\Exception;
use think\facade\Cache;

/**
 * @Apidoc\Title("超级用户管理")
 * @Apidoc\Group("SuperUser")
 * @Apidoc\Sort("5")
 */

class SuperUser extends AdminBaseController
{
  /**
   * @Apidoc\Title("超级用户列表")
   * @Apidoc\Desc("获取超级用户列表接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/get_super_users")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("列表")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="SuperUser", desc="权限验证" )
   * @Apidoc\Param( ref="pagingParam")
   * @Apidoc\Param("username", type="string",require=false, desc="用户名查询" )
   * @Apidoc\Returned("data", type="object", desc="业务数据",
   *    @Apidoc\Returned( ref="pagingParam"),
   *    @Apidoc\Returned("list", type="array",ref="app\common\model\SuperUser\getSuperUserList", desc="数据列表")
   * )
   */
  public function getSuperUsers(){
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    try {
      $qurey_info = $this->request->param();
      $result = $this->super_user_model->getSuperUserList($qurey_info, $this->super_user_auth);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("添加超级用户")
   * @Apidoc\Desc("添加超级用户接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/add_super_user")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("添加")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="AddSuperUser", desc="权限验证" )
   * @Apidoc\Param("username", type="string",require=true, desc="用户名" )
   * @Apidoc\Param("nickname", type="string",require=true, desc="昵称" )
   * @Apidoc\Returned("data", type="boolean", desc="业务数据")
   */
  public function addSuperUser()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      validate(SuperUserValidate::class)->scene('add')->check($qurey_info);
    }catch (ValidateException $validateException){
      return Show::error($validateException->getError());
    }
    $result = $this->super_user_model->getSuperUserByUsername($qurey_info['username']);
    if ($result) {
      return Show::error('用户名已被注册');
    }
    try {
      $data = [
        'username' => $qurey_info['username'],
        'nickname' => $qurey_info['nickname'],
        'password' => md5('admin')
      ];
      $result = $this->super_user_model->saveSuperUser($data);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("重置超级用户密码")
   * @Apidoc\Desc("重置超级用户密码接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/reset_super_user_pwd")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("重置超级用户密码")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="ResetSuperUserPwd", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="用户ID" )
   * @Apidoc\Returned("data", type="boolean", desc="业务数据")
   */
  public function resetSuperUserPwd()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      $data = [
        'id' => $qurey_info['id'],
        'password' => md5('admin')
      ];
      $result = $this->super_user_model->updateSuperUser($data);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("修改超级用户状态")
   * @Apidoc\Desc("修改超级用户状态接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/status_super_user")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("修改数据状态")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="DelSuperUser", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="用户ID" )
   * @Apidoc\Param("status", type="int",require=true, desc="状态：1（正常），0（禁用），-1（删除）" )
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function statusSuperUser()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      $result = $this->super_user_model->statusSuperUser($qurey_info);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("超级用户分配角色")
   * @Apidoc\Desc("超级用户分配角色接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/set_role")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("超级用户分配角色")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="SetRole", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="用户ID" )
   * @Apidoc\Param("roleId", type="int",require=true, desc="角色ID" )
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function setRole()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      $result = $this->super_user_model->setRole($qurey_info);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * 获取用户信息
   */
  /**
   * @Apidoc\Title("获取超级用户信息")
   * @Apidoc\Desc("获取超级用户信息接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/get_super_user_info")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("数据详情")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="SuperUserCenter", desc="权限验证" )
   * @Apidoc\Param("username", type="string",require=true, desc="用户名" )
   * @Apidoc\Returned("data", type="object", desc="业务数据",
   *    @Apidoc\Returned("nickname", type="string", desc="用户昵称"),
   *    @Apidoc\Returned("avatar", type="string", desc="用户头像"),
   *    @Apidoc\Returned("office", type="string", desc="用户职业"),
   *    @Apidoc\Returned("introduction", type="string", desc="个人介绍")
   * )
   */
  public function getSuperUserInfo(){
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    try {
      $qurey_info = $this->request->param();
      $super_user = $this->super_user_model->getSuperUserByUsername($qurey_info['username']);
      $result = [
        'nickname'=>$super_user['nickname'],
        'avatar'=>$super_user['avatar'],
        'office'=>$super_user['office'],
        'introduction'=>$super_user['introduction']
      ];
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("保存用户头像")
   * @Apidoc\Desc("保存用户头像接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/save_avatar")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("更新")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="SuperUserCenter", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="用户ID" )
   * @Apidoc\Param("avatar", type="string",require=true, desc="用户头像url" )
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function saveAvatar()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      $result = $this->super_user_model->saveAvatar($qurey_info);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("保存用户信息")
   * @Apidoc\Desc("保存用户信息接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/save_super_user_info")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("更新")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="SuperUserCenter", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="用户ID" )
   * @Apidoc\Param( ref="app\common\model\SuperUser\saveSuperUserInfo")
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function saveSuperUserInfo()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      $result = $this->super_user_model->saveSuperUserInfo($qurey_info);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("获取用户绑定账号")
   * @Apidoc\Desc("获取用户绑定账号接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/get_super_user_bind_account")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("数据详情")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="SuperUserCenter", desc="权限验证" )
   * @Apidoc\Param("username", type="string",require=true, desc="用户名" )
   * @Apidoc\Returned("data", type="object", desc="业务数据",
   *    @Apidoc\Returned("email", type="string", desc="邮箱")
   * )
   */
  public function getSuperUserBindAccount(){
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    try {
      $qurey_info = $this->request->param();
      $super_user = $this->super_user_model->getSuperUserByUsername($qurey_info['username']);
      $result = [
        'email'=>$super_user['email'],
      ];
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
  /**
   * @Apidoc\Title("用户绑定邮箱")
   * @Apidoc\Desc("用户绑定邮箱接口")
   * @Apidoc\Author("ckmmd")
   * @Apidoc\Url("/admin/super_user/save_super_user_email")
   * @Apidoc\Method("POST")
   * @Apidoc\Tag("更新")
   * @Apidoc\Header( ref="auth")
   * @Apidoc\Header("auth", type="String",require=true, default="SuperUserCenter", desc="权限验证" )
   * @Apidoc\Param("id", type="int",require=true, desc="用户ID" )
   * @Apidoc\Param("email", type="string",require=true, desc="要绑定的邮箱" )
   * @Apidoc\Returned("data", type="int", desc="业务数据")
   */
  public function saveSuperUserEmail()
  {
    if (!$this->request->isPost()) {
      return Show::error('错误的请求');
    }
    $qurey_info = $this->request->param();
    try {
      validate(SuperUserValidate::class)->scene('save_email')->check($qurey_info);
    }catch (ValidateException $validateException){
      return Show::error($validateException->getError());
    }
    $code = Cache::store('redis')->get($qurey_info['username'].$qurey_info['code']);
    if (!$code) return Show::error('验证码错误');
    try {
      $super_user = $this->super_user_model->getSuperUserByEmail($qurey_info['email']);
      if (!empty($super_user)) {
        return Show::error('邮箱账号已经绑定其他用户');
      }
      $result = $this->super_user_model->saveSuperUserEmail($qurey_info);
      return Show::success($result);
    } catch (Exception $e) {
      return Show::error($e->getMessage());
    }
  }
}